// 组件创建前
async function defineComponentBeforeCreated({ VueElement }) {
  try {
    if (VueElement.component.props && VueElement.component.props.mapInfo) {
      window[`${VueElement.id}mapGepJson`] = VueElement.component.props.mapInfo.json
    } else {
      let mapUrl = BASE_URL + "/adminapp/public/assets/elfinder/files/json/geo/中国.json"
      let getMapJson = function () {
        return fetch(mapUrl).then(res => res.json())
      }
      window[`${VueElement.id}mapGepJson`] = await getMapJson()
    }
    echarts.registerMap(VueElement.id, window[`${VueElement.id}mapGepJson`])
  } catch (error) {
    console.log("error1", error);
  }
}

// 组件创建后
async function defineComponentCreated({ VueElement, VueInstance }) {
  try {
    if (VueElement && VueInstance) {
      newMapDate = VueElement.id
      if (newMapDate) {
        VueInstance.options.series[0].map = newMapDate
        VueInstance.options.geo.map = newMapDate
        echarts.registerMap(newMapDate, window[`${VueElement.id}mapGepJson`])
      } else {
        echarts.registerMap(newMapDate, window[`${VueElement.id}mapGepJson`])
      }
    }
  } catch (error) {
    console.log("error2", error);
  }

}

async function updateMap(data, VueInstance) {
  let mapGeoJson = data
  echarts.registerMap(VueInstance.options.series[0].map, mapGeoJson);
  mapDataRender = mapGeoJson.features.map((item) => {
    return {
      value: (Math.random() * 1000).toFixed(2),
      name: item.properties.name,
    };
  });
  if (VueInstance) {
    VueInstance.options.series[0].data = mapDataRender
    VueInstance.updateOptions(VueInstance.options)
  }
}

async function handleDataCallback({
  ResourceData,
  VueInstance,
}) {
  if (VueInstance) {
    updateMap(ResourceData, VueInstance)
  }
}